{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: llama_index.llms.openai_like in d:\\python3.10\\lib\\site-packages (0.1.3)\n",
      "Requirement already satisfied: llama-index-core<0.11.0,>=0.10.1 in d:\\python3.10\\lib\\site-packages (from llama_index.llms.openai_like) (0.10.38.post2)\n",
      "Requirement already satisfied: llama-index-llms-openai<0.2.0,>=0.1.1 in d:\\python3.10\\lib\\site-packages (from llama_index.llms.openai_like) (0.1.20)\n",
      "Requirement already satisfied: transformers<5.0.0,>=4.37.0 in d:\\python3.10\\lib\\site-packages (from llama_index.llms.openai_like) (4.41.1)\n",
      "Requirement already satisfied: PyYAML>=6.0.1 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (6.0.1)\n",
      "Requirement already satisfied: SQLAlchemy[asyncio]>=1.4.49 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2.0.30)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.6 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (3.9.5)\n",
      "Requirement already satisfied: dataclasses-json in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (0.5.14)\n",
      "Requirement already satisfied: deprecated>=1.2.9.3 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.2.14)\n",
      "Requirement already satisfied: dirtyjson<2.0.0,>=1.0.8 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.0.8)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2023.9.0)\n",
      "Requirement already satisfied: httpx in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (0.27.0)\n",
      "Requirement already satisfied: llamaindex-py-client<0.2.0,>=0.1.18 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (0.1.19)\n",
      "Requirement already satisfied: nest-asyncio<2.0.0,>=1.5.8 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.6.0)\n",
      "Requirement already satisfied: networkx>=3.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (3.1)\n",
      "Requirement already satisfied: nltk<4.0.0,>=3.8.1 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (3.8.1)\n",
      "Requirement already satisfied: numpy in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.25.2)\n",
      "Requirement already satisfied: openai>=1.1.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.30.2)\n",
      "Requirement already satisfied: pandas in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2.2.2)\n",
      "Requirement already satisfied: pillow>=9.0.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (10.0.0)\n",
      "Requirement already satisfied: requests>=2.31.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2.31.0)\n",
      "Requirement already satisfied: tenacity<9.0.0,>=8.2.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (8.2.3)\n",
      "Requirement already satisfied: tiktoken>=0.3.3 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (0.7.0)\n",
      "Requirement already satisfied: tqdm<5.0.0,>=4.66.1 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (4.66.1)\n",
      "Requirement already satisfied: typing-extensions>=4.5.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (4.7.1)\n",
      "Requirement already satisfied: typing-inspect>=0.8.0 in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (0.9.0)\n",
      "Requirement already satisfied: wrapt in d:\\python3.10\\lib\\site-packages (from llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.16.0)\n",
      "Requirement already satisfied: filelock in d:\\python3.10\\lib\\site-packages (from transformers<5.0.0,>=4.37.0->llama_index.llms.openai_like) (3.12.3)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.23.0 in d:\\python3.10\\lib\\site-packages (from transformers<5.0.0,>=4.37.0->llama_index.llms.openai_like) (0.23.1)\n",
      "Requirement already satisfied: packaging>=20.0 in d:\\python3.10\\lib\\site-packages (from transformers<5.0.0,>=4.37.0->llama_index.llms.openai_like) (23.1)\n",
      "Requirement already satisfied: regex!=2019.12.17 in d:\\python3.10\\lib\\site-packages (from transformers<5.0.0,>=4.37.0->llama_index.llms.openai_like) (2023.8.8)\n",
      "Requirement already satisfied: tokenizers<0.20,>=0.19 in d:\\python3.10\\lib\\site-packages (from transformers<5.0.0,>=4.37.0->llama_index.llms.openai_like) (0.19.1)\n",
      "Requirement already satisfied: safetensors>=0.4.1 in d:\\python3.10\\lib\\site-packages (from transformers<5.0.0,>=4.37.0->llama_index.llms.openai_like) (0.4.3)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in d:\\python3.10\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in d:\\python3.10\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (23.1.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in d:\\python3.10\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.4.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in d:\\python3.10\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (6.0.4)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in d:\\python3.10\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.9.2)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0 in d:\\python3.10\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (4.0.3)\n",
      "Requirement already satisfied: pydantic>=1.10 in d:\\python3.10\\lib\\site-packages (from llamaindex-py-client<0.2.0,>=0.1.18->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.10.12)\n",
      "Requirement already satisfied: anyio in d:\\python3.10\\lib\\site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (3.7.1)\n",
      "Requirement already satisfied: certifi in d:\\python3.10\\lib\\site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2023.7.22)\n",
      "Requirement already satisfied: httpcore==1.* in d:\\python3.10\\lib\\site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.0.5)\n",
      "Requirement already satisfied: idna in d:\\python3.10\\lib\\site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (3.4)\n",
      "Requirement already satisfied: sniffio in d:\\python3.10\\lib\\site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.3.0)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in d:\\python3.10\\lib\\site-packages (from httpcore==1.*->httpx->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (0.14.0)\n",
      "Requirement already satisfied: click in d:\\python3.10\\lib\\site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (8.1.7)\n",
      "Requirement already satisfied: joblib in d:\\python3.10\\lib\\site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.3.2)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in d:\\python3.10\\lib\\site-packages (from openai>=1.1.0->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.9.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in d:\\python3.10\\lib\\site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (3.2.0)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\python3.10\\lib\\site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2.0.4)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in d:\\python3.10\\lib\\site-packages (from SQLAlchemy[asyncio]>=1.4.49->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2.0.2)\n",
      "Requirement already satisfied: colorama in d:\\python3.10\\lib\\site-packages (from tqdm<5.0.0,>=4.66.1->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (0.4.6)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in d:\\python3.10\\lib\\site-packages (from typing-inspect>=0.8.0->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.0.0)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in d:\\python3.10\\lib\\site-packages (from dataclasses-json->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (3.20.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in d:\\python3.10\\lib\\site-packages (from pandas->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in d:\\python3.10\\lib\\site-packages (from pandas->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.7 in d:\\python3.10\\lib\\site-packages (from pandas->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (2024.1)\n",
      "Requirement already satisfied: exceptiongroup in d:\\python3.10\\lib\\site-packages (from anyio->httpx->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.1.3)\n",
      "Requirement already satisfied: six>=1.5 in d:\\python3.10\\lib\\site-packages (from python-dateutil>=2.8.2->pandas->llama-index-core<0.11.0,>=0.10.1->llama_index.llms.openai_like) (1.16.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 23.2.1 -> 24.0\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "%pip install llama_index.llms.openai_like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数字孪生（Digital Twin）是一种先进的建模技术，它通过创建物理实体（如机器、设备、系统或过程）的虚拟副本，来模拟和分析其在现实世界中的行为和性能。这种技术结合了物联网（IoT）、大数据分析、人工智能（AI）和机器学习等技术，以实现对物理实体的实时监控、预测性维护、性能优化和决策支持。\n",
      "\n",
      "数字孪生的核心思想是在数字世界中复制一个物理对象或系统，使得用户可以在不影响实际操作的情况下，对其进行测试、分析和优化。数字孪生可以用于多种场景，包括但不限于：\n",
      "\n",
      "1. 制造业：通过数字孪生技术，制造商可以模拟生产线，优化生产流程，减少停机时间，提高产品质量。\n",
      "2. 航空航天：飞机和航天器的数字孪生可以帮助工程师在设计阶段进行仿真测试，预测潜在问题，并在实际飞行前进行改进。\n",
      "3. 城市规划：城市或基础设施的数字孪生可以用于模拟交通流量、能源消耗和环境影响，以支持可持续发展和智慧城市的建设。\n",
      "4. 医疗健康：通过创建人体的数字孪生，医生可以模拟手术过程，预测治疗效果，为患者提供个性化的治疗方案。\n",
      "\n",
      "数字孪生的实现通常涉及以下几个步骤：\n",
      "\n",
      "1. 数据采集：通过传感器和其他数据采集设备收集物理实体的运行数据。\n",
      "2. 模型创建：使用收集到的数据创建物理实体的数字模型。\n",
      "3. 数据集成：将实时数据与数字模型集成，以保持模型的准确性和实时性。\n",
      "4. 分析与优化：利用大数据分析和机器学习技术对数字孪生进行分析，以发现潜在问题并提出优化建议。\n",
      "5. 决策支持：基于数字孪生的分析结果，支持决策者做出更明智的决策。\n",
      "\n",
      "随着技术的不断进步，数字孪生正在成为推动工业4.0和数字化转型的重要工具。\n"
     ]
    }
   ],
   "source": [
    "from llama_index.llms.openai_like import OpenAILike\n",
    "\n",
    "llm = OpenAILike(\n",
    "    model=\"openai/deepseek-chat\", \n",
    "    api_base=\"http://1.15.125.13:3033/v1\", \n",
    "    api_key=\"sk-1234\")\n",
    "\n",
    "response = llm.complete(\"什么是数字孪生？\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR: Could not find a version that satisfies the requirement llama_index.core.tools (from versions: none)\n",
      "ERROR: No matching distribution found for llama_index.core.tools\n",
      "\n",
      "[notice] A new release of pip is available: 23.2.1 -> 24.0\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "%pip install llama_index.core.tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.tools import FunctionTool\n",
    "\n",
    "def add(x: int, y: int) -> int:\n",
    "    \"\"\" 两个整数相加 \"\"\"\n",
    "    return x + y\n",
    "\n",
    "def mystery( x: int, y: int) -> int:\n",
    "    \"\"\"\" 一个神奇的函数 \"\"\"\n",
    "    return (x+y)*(x+y)\n",
    "\n",
    "def xiaojian(x: int, y: int) -> str:\n",
    "    \"\"\"这是一个laobao函数\"\"\"\n",
    "    return \"laobao says: \"+str(x*y)\n",
    "\n",
    "\n",
    "add_tool = FunctionTool.from_defaults(fn=add)\n",
    "mystery_tool = FunctionTool.from_defaults(fn=mystery)\n",
    "xiaojian_tool = FunctionTool.from_defaults(fn=xiaojian)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;3;38;5;200mThought: The current language of the user is: Chinese. I need to use the mystery tool to find the result of 9 and 8.\n",
      "Action: mystery\n",
      "Action Input: {'x': 9, 'y': 8}\n",
      "\u001b[0m\u001b[1;3;34mObservation: 289\n",
      "\u001b[0m289\n"
     ]
    }
   ],
   "source": [
    "response = llm.predict_and_call(\n",
    "    [add_tool, mystery_tool, xiaojian_tool],\n",
    "    # \"将9和8laobao一下\",\n",
    "    # \"将9和8处理一下，需要输出为文本\",\n",
    "    \"9和8神奇的结果是什么呢？\",\n",
    "    verbose=True\n",
    ")\n",
    "\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
